package cn.com.chinahitech.bjmarket.user.controller;


import cn.com.chinahitech.bjmarket.user.entity.User;
import cn.com.chinahitech.bjmarket.user.service.IUserService;
import cn.com.chinahitech.bjmarket.utils.Constants;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhufu
 * @since 2020-07-07
 */
@Controller
@RequestMapping("/user")
@Api(value="用户信息接口",tags = {"用户信息的Controller"})
public class UserController {
    @Autowired
    private IUserService userService;

    @RequestMapping(value="/getUserByName",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="根据用户姓氏检索用户列表",notes = "用户列表查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name="name",value="姓氏",required = true,dataType = "String")
    })
    @ApiResponses({
            @ApiResponse(code=-1,message="请求失败"),
            @ApiResponse(code=200,message="请求成功")
    })
    public String getUserByName(String name){
        List<User>  userList =null;
        Map<String,Object> map =new HashMap<String,Object>();
        try{
            userList=userService.getUserByName(name);
            map.put("status","200");
            map.put("data",userList);
        }catch(Exception ex){
            map.put("status","-1");
            map.put("errorMsg",ex.getMessage());
        }
        return JSON.toJSONString(map);
    }
    @RequestMapping(value="/getUserById",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="根据id检索用户信息",notes = "用户对象查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name="uid",value="用户编号",required = true,dataType = "String")
    })
    @ApiResponses({
            @ApiResponse(code=-1,message="请求失败"),
            @ApiResponse(code=200,message="请求成功")
    })
    public String getUserById(String uid){
        User user = null;
        Map<String,Object> map = new HashMap<String,Object>();
        try{
            user = userService.getUeserByUid(uid);
            map.put("status","200");
            map.put("data",user);
        }catch (Exception ex){
            map.put("status","-1");
            map.put("errorMsg",ex.getMessage());
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping(value="/page",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="分页检索用户列表",notes = "分页检索")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value="页码",required = true,dataType = "Integer"),
            @ApiImplicitParam(name="name",value="姓氏",required = true,dataType = "String")
    })
    @ApiResponses({
            @ApiResponse(code=-1,message="请求失败"),
            @ApiResponse(code=200,message="请求成功")
    })
    public String queryAtPage(int page,String name){
        IPage<User> result =null;
        Map<String,Object> map = new HashMap<String,Object>();
        try{
            result =userService.getUserAtPage(page, Constants.PAGE_SIZE,name);
            map.put("status","200");
            map.put("data",result);
        }catch (Exception ex){
            map.put("status","-1");
            map.put("errorMsg",ex.getMessage());
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping(value="/updatePhone",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="更新用户phone",notes = "更新用户的手机号")
    @ApiImplicitParams({
            @ApiImplicitParam(name="uid",value="用户编号",required = true,dataType = "String"),
            @ApiImplicitParam(name="phoneNumber",value="新手机号",required = true,dataType = "String")
    })
    @ApiResponses({
            @ApiResponse(code=-1,message="请求失败"),
            @ApiResponse(code=200,message="请求成功")
    })
    public String updatePhone(String uid,String phoneNumber){
        Map<String,Object> map = new HashMap<String,Object>();
        try{
            userService.updatePhone(phoneNumber,uid);
            map.put("status","200");
            map.put("msg","更新成功");
        }catch(Exception ex){
            map.put("status","-1");
            map.put("errorMsg",ex.getMessage());
        }
        return JSON.toJSONString(map);
    }
}
